package com.qipay.baas.mapper;

import com.qipay.baas.model.SmsCode;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface SmsCodeMapper {
    @Delete({
        "delete from sms_code",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into sms_code (mobile, code, ",
        "is_used, insert_time, ",
        "msg_id, `type`, try_count)",
        "values (#{mobile,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}, ",
        "#{isUsed,jdbcType=BIT}, #{insertTime,jdbcType=TIMESTAMP}, ",
        "#{msgId,jdbcType=INTEGER}, #{type,jdbcType=TINYINT}, #{tryCount,jdbcType=INTEGER})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(SmsCode record);

    @InsertProvider(type=SmsCodeSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insertSelective(SmsCode record);

    @Select({
        "select",
        "id, mobile, code, is_used, insert_time, msg_id, `type`, try_count",
        "from sms_code",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="mobile", property="mobile", jdbcType=JdbcType.VARCHAR),
        @Result(column="code", property="code", jdbcType=JdbcType.VARCHAR),
        @Result(column="is_used", property="isUsed", jdbcType=JdbcType.BIT),
        @Result(column="insert_time", property="insertTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="msg_id", property="msgId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.TINYINT),
        @Result(column="try_count", property="tryCount", jdbcType=JdbcType.INTEGER)
    })
    SmsCode selectByPrimaryKey(Integer id);

    @UpdateProvider(type=SmsCodeSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SmsCode record);

    @Update({
        "update sms_code",
        "set mobile = #{mobile,jdbcType=VARCHAR},",
          "code = #{code,jdbcType=VARCHAR},",
          "is_used = #{isUsed,jdbcType=BIT},",
          "insert_time = #{insertTime,jdbcType=TIMESTAMP},",
          "msg_id = #{msgId,jdbcType=INTEGER},",
          "`type` = #{type,jdbcType=TINYINT},",
          "try_count = #{tryCount,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(SmsCode record);
}